<?php
	include 'db.php'; //database config and connection	
	$grp = explode(',',$_GET['grp']);	
	//echo $grp[1];
	foreach ($grp as $itemId){
			$id_session = session_id(); 
    		$result = mysql_query("select count(*) from cart where sessionId = '" . $id_session . "' and itemId = '$itemId'");
			$row = mysql_fetch_row($result);
			$numRows = $row[0];
		 
			if($numRows == 0) {
				//This item doesn't exist in the users cart,
				//we will add it with an insert query
				@mysql_query("insert into cart(sessionId, itemId, qty) values('" . $id_session . "', $itemId, 1)");
				//header("Location: index.php");
			}
			else {
				//This item already exists in the users cart,
				//we will update it instead
				UpdateProduct($itemId);
			}
	}
	header("Location: products.php");
	
	function UpdateProduct($itemId) {
    	$id_session = session_id();
    	@mysql_query("update cart set qty = qty + 1 where sessionId = '" . $id_session . "' and itemId = '$itemId'");
    	
	}
?>